<?php

namespace App\Http\Controllers\Admin;

use App\Http\Config\ErrorCode;
use App\Models\Goods;
use Illuminate\Http\Request;


class GoodsController extends BaseController
{
    protected $fields = [
        'goods_name' => '',
        'goods_pic' => '',
        'pic_list' => '',
        'goods_type' => '',
        'body' => '',
        'status' => '',
        'goods_top' => '',
        'price_type' => ''
    ];

    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    public function index()
    {
        return response()->json($this->returnData);
    }

    /* 商品列表 */
    public function dataList(Request $request)
    {
        if (!$request->ajax()) {
            $this->returnData = ErrorCode::$admin_enum['request_error'];
            return response()->json($this->returnData);
        }
        $page_no = $request->post('page_no', 1);
        $page_size = $request->post('page_size', 10);
        $searchFilter = array(
            'sortName' => $request->post('sortName','id'),                                                  //排序列名
            'sortOrder' => $request->post('sortOrder','asc'),                                               //排序（desc，asc）
            'pageNumber' => $page_no,                                                                                   //当前页码
            'pageSize' => $page_size,                                                                                   //一页显示的条数
            'start' => ($page_no-1) * $page_size,                                                                       //开始位置
            'searchKey' => trim($request->post('search','')),                                               //搜索条件
            'is_del' => 0
        );
        $goodsModel = new Goods();
        $data = $goodsModel->getGoodsWithFilter($searchFilter);
        $this->returnData['data'] = $data;
        return response()->json($this->returnData);
    }

    /* 全部商品列表 */
    public function goodsList()
    {
        $goodsModel = new Goods();
        $data = $goodsModel->getGoodsList(['id','goods_name']);
        $this->returnData['data'] = $data;
        return response()->json($this->returnData);
    }

    /* 商品详情 */
    public function detail($id)
    {
        $userModel = new Goods();
        $data = $userModel->getGoodByID($id);
        if(!$data){
            $this->returnData = ErrorCode::$admin_enum['not_exist'];
            $this->returnData['msg'] = '数据不存在';
            return response()->json($this->returnData);
        }
        $this->returnData['data'] = $data;
        return response()->json($this->returnData);
    }

    /* 添加商品 */
    public function create(Request $request)
    {
        $goods = [];
        $goods_attr = $request->post('goods_attr');
        if($goods_attr==null){
            $this->returnData = ErrorCode::$admin_enum['params_error'];
            $this->returnData['msg'] = '产品规格不能为空';
            return response()->json($this->returnData);
        }
        $goods_attr = json_decode($goods_attr,true);
        foreach ($this->fields as $key=>$value) {
            /* 验证参数未做 */
            $goods[$key] = $request->post($key);
        }
        $tmp_arr = [];
        if(count($goods_attr)<1){
            $this->returnData = ErrorCode::$admin_enum['params_error'];
            $this->returnData['msg'] = '产品规格不能为空';
            return response()->json($this->returnData);
        }
        foreach ($goods_attr as $key=>$value){
            if(trim($value['goods_version'])==''){
                continue;
            }
            if(trim($value['time_length'])==''){
                continue;
            }
            if(trim($value['unit'])==''){
                continue;
            }
            if(trim($value['originalPrice'])==''){
                continue;
            }
            $tmp_arr[] = array(
                'goods_version' => $value['goods_version'],
                'time_length' => $value['time_length'],
                'unit' => $value['unit'],
                'originalPrice' => $value['originalPrice']
            );
        }
        $goods['is_del'] = 0;
        $goods['goods_version'] = json_encode($tmp_arr);
        $goodsModel = new Goods();
        $res = $goodsModel->goodsInsert($goods);
        if(!$res){
            $this->returnData = ErrorCode::$admin_enum['fail'];
            $this->returnData['msg'] = '添加失败';
            return response()->json($this->returnData);
        }
        $this->returnData['msg'] = '添加成功';
        return response()->json($this->returnData);
    }

    /* 修改商品 */
    public function edit(Request $request)
    {
        $id = $request->id;
        $goodsModel = new Goods();
        $data = $goodsModel->getGoodByID((int)$id);
        if (!$data){
            $this->returnData = ErrorCode::$admin_enum['fail'];
            $this->returnData['msg'] = '数据不存在';
            return response()->json($this->returnData);
        }
        $goods = [];
        $goods_attr = $request->post('goods_attr');
        if($goods_attr==null){
            $this->returnData = ErrorCode::$admin_enum['params_error'];
            $this->returnData['msg'] = '产品规格不能为空';
            return response()->json($this->returnData);
        }
        $goods_attr = json_decode($goods_attr,true);
        foreach ($this->fields as $key=>$value) {
            $goods[$key] = $request->post($key);
        }
        $tmp_arr = [];
        if(count($goods_attr)<1){
            $this->returnData = ErrorCode::$admin_enum['params_error'];
            $this->returnData['msg'] = '产品规格不能为空';
            return response()->json($this->returnData);
        }
        foreach ($goods_attr as $key=>$value){
            if(trim($value['goods_version'])==''){
                continue;
            }
            if(trim($value['time_length'])==''){
                continue;
            }
            if(trim($value['unit'])==''){
                continue;
            }
            if(trim($value['originalPrice'])==''){
                continue;
            }
            $tmp_arr[] = array(
                'goods_version' => $value['goods_version'],
                'time_length' => $value['time_length'],
                'unit' => $value['unit'],
                'originalPrice' => $value['originalPrice']
            );
        }
        $goods['goods_version'] = json_encode($tmp_arr);
        $goodsModel = new Goods();
        $res = $goodsModel->goodsUpdate($id,$goods);
        if(!$res){
            $this->returnData = ErrorCode::$admin_enum['fail'];
            $this->returnData['msg'] = '修改失败';
            return response()->json($this->returnData);
        }
        $this->returnData['msg'] = '修改成功';
        return response()->json($this->returnData);
    }

    /* 商品操作 */
    public function ajax(Request $request)
    {
        $id = $request->id;
        if (!$request->ajax()) {
            $this->returnData = ErrorCode::$admin_enum['request_error'];
            return response()->json($this->returnData);
        }
        if (!isset($request->action) || !in_array($request->action,['status'],true)){
            $this->returnData = ErrorCode::$admin_enum['params_error'];
            return response()->json($this->returnData);
        }
        $status = $request->post('status','');
        if(!in_array(strval($status),['0','1'],true)){
            $this->returnData = ErrorCode::$admin_enum['params_error'];
            return response()->json($this->returnData);
        }
        $goodsModel = new Goods();
        $res = $goodsModel->goodsUpdate($id,['status'=>$status]);
        if(!$res){
            $this->returnData = ErrorCode::$admin_enum['fail'];
            $this->returnData['msg'] = '更新失败';
            return response()->json($this->returnData);
        }
        $this->returnData['msg'] = '更新成功';
        return response()->json($this->returnData);
    }
    
    /* 删除商品 */
    public function delete($id)
    {
        $data = array(
            'is_del' => 1,
            'status' => 0
        );
        $goodsModel = new Goods();
        $res = $goodsModel->goodsUpdate($id,$data);
        if(!$res){
            $this->returnData = ErrorCode::$admin_enum['fail'];
            $this->returnData['msg'] = '删除失败';
            return response()->json($this->returnData);
        }
        $this->returnData['msg'] = '删除成功';
        return response()->json($this->returnData);
    }
}
